package server.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import server.domain.Page;
import server.domain.Standard;
import server.mapper.StandardMapper;

@Service
public class StandardService {
	@Autowired
	private StandardMapper mapper;

	public List<Standard> getAll(){
		return mapper.selectAll();
	}
	public boolean insert(Standard s) {
		int result = mapper.insert(s);
		return result==0 ? false:true;
	}
	public int getCount() {
		return mapper.getCount();
	}
	// 分页查询某一页数据
	public Page<Standard> findPage(Integer page, Integer size) {
		int total = mapper.getCount();

		List<Standard> items = mapper.selectPage((page - 1) * size, size);

		Page<Standard> pageData = new Page<>();
		pageData.setTotal(total);
		pageData.setItems(items);

		return pageData;
	}
	public boolean delete(Standard s) {
		if(s==null) {
			return false;
		}
		int result=mapper.deleteByPrimaryKey(s.getId());
		return result==0 ? false : true;
	}
	public boolean update(Standard s) {
		if(s==null) {
			return false;
		}
		int result = mapper.updateByPrimaryKey(s);
		return result==0 ? false : true;
	}
	public boolean delBatch(Integer[] ids) {
		if(ids==null||ids.length==0)
			return false;
		int rs=mapper.delBatch(ids);
		return rs==0 ? false : true;
	}

	public Page<Standard> search(String query,int page,int size) {
		if (page <= 0) page = 1;
		if (size <= 0) size = 10;

		if (query != null && query.length() != 0) {
			query = "%" + query + "%";
		}

		List<Standard> list=mapper.search(query,(page - 1) * size,size);
		int total=mapper.searchTotal(query);

		Page<Standard> pageData=new Page<Standard>();
		pageData.setItems(list);
		pageData.setTotal(total);

		return pageData;
	}
	public Integer searchTotal(String name) {
		return mapper.searchTotal(name);
	}
}
